package com.appsmith.server.helpers;

import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginException;
import com.appsmith.external.models.Datasource;
import com.appsmith.external.models.DatasourceConfiguration;
import com.appsmith.external.models.DatasourceStorage;
import com.appsmith.external.models.DatasourceTestResult;
import com.appsmith.external.models.OAuth2;
import com.appsmith.server.constants.AnalyticsConstants;
import org.apache.commons.lang3.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

import static com.appsmith.server.constants.ce.AnalyticsConstantsCE.DATASOURCE_CREATED_AT_SHORTNAME;
import static com.appsmith.server.constants.ce.AnalyticsConstantsCE.DATASOURCE_ID_SHORTNAME;
import static com.appsmith.server.constants.ce.AnalyticsConstantsCE.DATASOURCE_IS_MOCK_SHORTNAME;
import static com.appsmith.server.constants.ce.AnalyticsConstantsCE.DATASOURCE_IS_TEMPLATE_SHORTNAME;
import static com.appsmith.server.constants.ce.AnalyticsConstantsCE.DATASOURCE_NAME_SHORTNAME;
import static com.appsmith.server.constants.ce.AnalyticsConstantsCE.ENVIRONMENT_ID_SHORTNAME;
import static com.appsmith.server.constants.ce.AnalyticsConstantsCE.ENVIRONMENT_NAME_SHORTNAME;

public class DatasourceAnalyticsUtils {

    public static Map<String, Object> getAnalyticsProperties(Datasource datasource) {
        Map<String, Object> analyticsProperties = new HashMap<>();
        analyticsProperties.put("pluginName", datasource.getPluginName());
        analyticsProperties.put("pluginId", datasource.getPluginId());
        analyticsProperties.put("dsName", datasource.getName());
        analyticsProperties.put("workspaceId", datasource.getWorkspaceId());
        analyticsProperties.put("isAutoGenerated", datasource.getIsAutoGenerated());
        analyticsProperties.put("dsIsTemplate", ObjectUtils.defaultIfNull(datasource.getIsTemplate(), ""));
        analyticsProperties.put("dsIsMock", ObjectUtils.defaultIfNull(datasource.getIsMock(), ""));
        return analyticsProperties;
    }

    public static Map<String, Object> getAnalyticsProperties(DatasourceStorage datasourceStroge) {
        Map<String, Object> analyticsProperties = new HashMap<>();
        analyticsProperties.put("pluginName", datasourceStroge.getPluginName());
        analyticsProperties.put("pluginId", datasourceStroge.getPluginId());
        analyticsProperties.put("dsName", datasourceStroge.getName());
        analyticsProperties.put("workspaceId", datasourceStroge.getWorkspaceId());
        analyticsProperties.put("isAutoGenerated", datasourceStroge.getIsAutoGenerated());
        analyticsProperties.put("dsIsTemplate", ObjectUtils.defaultIfNull(datasourceStroge.getIsTemplate(), ""));
        analyticsProperties.put("dsIsMock", ObjectUtils.defaultIfNull(datasourceStroge.getIsMock(), ""));
        return analyticsProperties;
    }

    public static Map<String, Object> getAnalyticsPropertiesForTestEventStatus(
            DatasourceStorage datasourceStorage, boolean status, Throwable e, String environmentName) {
        Map<String, Object> analyticsProperties = getAnalyticsPropertiesWithStorage(datasourceStorage, environmentName);
        analyticsProperties.put("isSuccess", status);

        if (e == null) {
            return analyticsProperties;
        }

        if (e instanceof AppsmithPluginException) {
            analyticsProperties.put("errorCode", ((AppsmithPluginException) e).getAppErrorCode());
        }
        analyticsProperties.put("errors", e.getMessage());
        return analyticsProperties;
    }

    public static Map<String, Object> getAnalyticsPropertiesForTestEventStatus(
            DatasourceStorage datasourceStorage, boolean status, String environmentName) {
        Map<String, Object> analyticsProperties = getAnalyticsPropertiesWithStorage(datasourceStorage, environmentName);
        analyticsProperties.put("isSuccess", status);
        analyticsProperties.put("errors", datasourceStorage.getInvalids());
        return analyticsProperties;
    }

    public static Map<String, Object> getAnalyticsPropertiesForTestEventStatus(
            DatasourceStorage datasourceStorage, DatasourceTestResult datasourceTestResult, String environmentName) {
        Map<String, Object> analyticsProperties = getAnalyticsPropertiesWithStorage(datasourceStorage, environmentName);
        analyticsProperties.put("isSuccess", datasourceTestResult.isSuccess());
        analyticsProperties.put("errors", datasourceTestResult.getInvalids());
        return analyticsProperties;
    }

    public static Map<String, Object> getAnalyticsPropertiesWithStorage(
            DatasourceStorage datasourceStorage, String environmentName) {
        Map<String, Object> analyticsProperties = new HashMap<>();
        analyticsProperties.putAll(getAnalyticsProperties(datasourceStorage));
        analyticsProperties.put("pluginName", datasourceStorage.getPluginName());
        analyticsProperties.put("dsName", datasourceStorage.getName());
        analyticsProperties.put("envId", datasourceStorage.getEnvironmentId());
        analyticsProperties.put(AnalyticsConstants.ENVIRONMENT_NAME_SHORTNAME, environmentName);
        DatasourceConfiguration dsConfig = datasourceStorage.getDatasourceConfiguration();
        if (dsConfig != null
                && dsConfig.getAuthentication() != null
                && dsConfig.getAuthentication() instanceof OAuth2) {
            analyticsProperties.put("oAuthStatus", dsConfig.getAuthentication().getAuthenticationStatus());
        }
        return analyticsProperties;
    }

    public static Map<String, Object> getAnalyticsPropertiesWithStorageOnActionExecution(
            DatasourceStorage datasourceStorage, String dsCreatedAt, String environmentName) {
        Map<String, Object> data = new HashMap<>();

        data.put(DATASOURCE_ID_SHORTNAME, ObjectUtils.defaultIfNull(datasourceStorage.getDatasourceId(), ""));
        data.put(ENVIRONMENT_ID_SHORTNAME, ObjectUtils.defaultIfNull(datasourceStorage.getEnvironmentId(), ""));
        data.put(DATASOURCE_NAME_SHORTNAME, datasourceStorage.getName());
        data.put(DATASOURCE_IS_TEMPLATE_SHORTNAME, ObjectUtils.defaultIfNull(datasourceStorage.getIsTemplate(), ""));
        data.put(DATASOURCE_IS_MOCK_SHORTNAME, ObjectUtils.defaultIfNull(datasourceStorage.getIsMock(), ""));
        data.put(DATASOURCE_CREATED_AT_SHORTNAME, dsCreatedAt);
        data.put(ENVIRONMENT_NAME_SHORTNAME, ObjectUtils.defaultIfNull(environmentName, ""));

        return data;
    }
}
